package com.rickpan.repository;

import com.rickpan.entity.TransferRecord;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

/**
 * 传输记录数据访问层
 */
@Repository
public interface TransferRecordRepository extends JpaRepository<TransferRecord, Long> {

    /**
     * 根据用户ID查询传输记录
     */
    Page<TransferRecord> findByUserIdOrderByCreatedAtDesc(Long userId, Pageable pageable);

    /**
     * 根据用户ID和传输状态查询记录
     */
    List<TransferRecord> findByUserIdAndTransferStatusOrderByCreatedAtDesc(
            Long userId, TransferRecord.TransferStatus status);

    /**
     * 根据用户ID和传输状态查询记录（分页）
     */
    Page<TransferRecord> findByUserIdAndTransferStatusOrderByCreatedAtDesc(
            Long userId, TransferRecord.TransferStatus status, Pageable pageable);

    /**
     * 根据会话ID查询传输记录（返回最新的一条）
     */
    Optional<TransferRecord> findFirstBySessionIdOrderByCreatedAtDesc(String sessionId);

    /**
     * 根据会话ID查询所有传输记录
     */
    List<TransferRecord> findBySessionId(String sessionId);

    /**
     * 查询用户的进行中传输记录
     */
    @Query("SELECT tr FROM TransferRecord tr WHERE tr.userId = :userId " +
           "AND tr.transferStatus IN ('PENDING', 'IN_PROGRESS') " +
           "ORDER BY tr.createdAt DESC")
    List<TransferRecord> findActiveTransfersByUserId(@Param("userId") Long userId);

    /**
     * 根据传输类型和状态查询记录
     */
    Page<TransferRecord> findByUserIdAndTransferTypeAndTransferStatusOrderByCreatedAtDesc(
            Long userId, TransferRecord.TransferType transferType, 
            TransferRecord.TransferStatus status, Pageable pageable);

    /**
     * 查询指定时间范围内的传输记录
     */
    @Query("SELECT tr FROM TransferRecord tr WHERE tr.userId = :userId " +
           "AND tr.createdAt BETWEEN :startTime AND :endTime " +
           "ORDER BY tr.createdAt DESC")
    Page<TransferRecord> findByUserIdAndTimeRange(
            @Param("userId") Long userId,
            @Param("startTime") LocalDateTime startTime,
            @Param("endTime") LocalDateTime endTime,
            Pageable pageable);

    /**
     * 统计用户传输记录数量
     */
    @Query("SELECT COUNT(tr) FROM TransferRecord tr WHERE tr.userId = :userId " +
           "AND tr.transferStatus = :status")
    long countByUserIdAndStatus(@Param("userId") Long userId, 
                               @Param("status") TransferRecord.TransferStatus status);

    /**
     * 查询用户今日传输记录
     */
    @Query("SELECT tr FROM TransferRecord tr WHERE tr.userId = :userId " +
           "AND DATE(tr.createdAt) = CURRENT_DATE " +
           "ORDER BY tr.createdAt DESC")
    List<TransferRecord> findTodayTransfersByUserId(@Param("userId") Long userId);

    /**
     * 删除指定时间之前的传输记录（清理历史数据）
     */
    @Query("DELETE FROM TransferRecord tr WHERE tr.createdAt < :beforeTime")
    void deleteRecordsBeforeTime(@Param("beforeTime") LocalDateTime beforeTime);

    /**
     * 查询失败的传输记录（用于重试）
     */
    List<TransferRecord> findByUserIdAndTransferStatusAndCreatedAtAfterOrderByCreatedAtDesc(
            Long userId, TransferRecord.TransferStatus status, LocalDateTime after);
}
